//
//  HNSH_ExchangeDianFeiViewController.m
//  HNSH_SingleProject
//
//  Created by 李宇廷 on 2023/4/4.
//

#import "HNSH_ExchangeDianFeiViewController.h"

#import "HNSH_ExchangeHuaFeiItem.h"

#import "ST_PayPasswordView.h"           //支付弹窗

#import "HNSH_ExchangeNetServer.h"

@interface HNSH_ExchangeDianFeiViewController ()

/* scrollView */
@property (nonatomic, strong) UIScrollView *scrollView;

/* addressField */
@property (nonatomic, strong) UITextField *addressField;
/* cardIdField */
@property (nonatomic, strong) UITextField *cardIdField;

/* 供电items */
@property (nonatomic, strong) NSMutableArray *gdItems;
/* 类型items */
@property (nonatomic, strong) NSMutableArray *typeItems;
/* moneyItems */
@property (nonatomic, strong) NSMutableArray *moneyItems;

/* currentModel */
@property (nonatomic, strong) HNSH_ExchangeHuaFeiItemModel *currentModel;

/* 单位名称 */
@property (nonatomic, copy) NSString *dwmcString;
/* 类型 */
@property (nonatomic, copy) NSString *typeString;
@end

@implementation HNSH_ExchangeDianFeiViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    [self showNavTitle:@"电费充值" showLeftItem:YES];
    
    self.view.backgroundColor = [UIColor appWhiteLabelColor];
    
    self.dwmcString = @"";
    self.typeString = @"";
    
    [self addAllViews];
    
    HNSH_ExchangeHuaFeiItemModel *item = [[HNSH_ExchangeHuaFeiItemModel alloc]init];
    item.uid = @"1";
    item.price = @"100";
    item.name = @"名字";
    item.price = @"200";
    [self configJFViewWithMoneyItems:@[item,item,item,item]];
}

- (void)addAllViews {
    [self.view addSubview:self.scrollView];
    
    [self configDQView];
    
    [self configGDView];
    
    [self configTypeView];
    
    [self configRHHView];
    
    [self loadHomeScrollData];
}

//输入地区
- (void)configDQView {
    UILabel *titleLabel = [[UILabel alloc]initWithFrame:CGRectMake(16, 16, 200, 20)];
    titleLabel.text = @"请输入地区";
    titleLabel.font = [UIFont systemFontOfSize:14];
    titleLabel.textColor = [UIColor colorWithHexString:@"#666666"];
    [self.scrollView addSubview:titleLabel];
    
    self.addressField.frame = CGRectMake(16, titleLabel.height+titleLabel.origin.y+10, kScreenWidth-32, 28);
    [self.scrollView addSubview:self.addressField];
    
    UIView *lineView = [[UIView alloc]initWithFrame:(CGRect)CGRectMake(16, self.addressField.height+self.addressField.origin.y+8, kScreenWidth-32, 1)];
    lineView.backgroundColor = [UIColor colorWithHexString:@"#EDEDED"];
    [self.scrollView addSubview:lineView];
}

//供电单位
- (void)configGDView {
    UILabel *titleLabel = [[UILabel alloc]initWithFrame:CGRectMake(16, 110, 200, 20)];
    titleLabel.text = @"供电单位";
    titleLabel.font = [UIFont systemFontOfSize:14];
    titleLabel.textColor = [UIColor colorWithHexString:@"#666666"];
    [self.scrollView addSubview:titleLabel];
    
    NSArray *titles = @[@"国家电力公司",@"南网电力公司"];
    self.gdItems = [NSMutableArray arrayWithCapacity:2];
    CGFloat width = (kScreenWidth-48)/2;
    for (int i = 0; i < titles.count; i ++) {
        UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
        button.frame = CGRectMake(16+(16+width)*i, titleLabel.height+titleLabel.origin.y+10, width, 44);
        [button setTitle:titles[i] forState:UIControlStateNormal];
        [button setTitleColor:[UIColor colorWithHexString:@"#666666"] forState:UIControlStateNormal];
        [button setTitleColor:[UIColor appGreenColor] forState:UIControlStateSelected];
        button.titleLabel.font = [UIFont systemFontOfSize:12];
        button.layer.cornerRadius = 4;
        button.layer.masksToBounds = YES;
        button.layer.borderWidth = 1.0f;
        button.layer.borderColor = [[UIColor appBGColor] CGColor];
        button.backgroundColor = [UIColor appBGColor];
        [self.scrollView addSubview:button];
        [self.gdItems addObject:button];
        button.tag = i;
        [button addTarget:self action:@selector(gdButtonClick:) forControlEvents:UIControlEventTouchUpInside];
    }
}

- (void)gdButtonClick:(UIButton *)sender {
    for (UIButton *button in self.gdItems) {
        button.selected = NO;
        button.backgroundColor = [UIColor appBGColor];
        button.layer.borderColor = [[UIColor appBGColor] CGColor];
    }
    self.dwmcString = sender.titleLabel.text;
    sender.selected = YES;
    sender.backgroundColor = [UIColor colorWithHexString:@"#ECF9F9"];
    sender.layer.borderColor = [[UIColor appGreenColor] CGColor];
}

//类型
- (void)configTypeView {
    UILabel *titleLabel = [[UILabel alloc]initWithFrame:CGRectMake(16, 212, 200, 20)];
    titleLabel.text = @"类型";
    titleLabel.font = [UIFont systemFontOfSize:14];
    titleLabel.textColor = [UIColor colorWithHexString:@"#666666"];
    [self.scrollView addSubview:titleLabel];
    
    NSArray *titles = @[@"住宅",@"商铺",@"企事业"];
    self.typeItems = [NSMutableArray arrayWithCapacity:2];
    CGFloat width = (kScreenWidth-64)/3;
    for (int i = 0; i < titles.count; i ++) {
        UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
        button.frame = CGRectMake(16+(16+width)*i, titleLabel.height+titleLabel.origin.y+10, width, 44);
        [button setTitle:titles[i] forState:UIControlStateNormal];
        [button setTitleColor:[UIColor colorWithHexString:@"#666666"] forState:UIControlStateNormal];
        [button setTitleColor:[UIColor appGreenColor] forState:UIControlStateSelected];
        button.titleLabel.font = [UIFont systemFontOfSize:12];
        button.layer.cornerRadius = 4;
        button.layer.masksToBounds = YES;
        button.layer.borderWidth = 1.0f;
        button.layer.borderColor = [[UIColor appBGColor] CGColor];
        button.backgroundColor = [UIColor appBGColor];
        [self.scrollView addSubview:button];
        [self.typeItems addObject:button];
        button.tag = i;
        [button addTarget:self action:@selector(typeButtonClick:) forControlEvents:UIControlEventTouchUpInside];
    }
}

- (void)typeButtonClick:(UIButton *)sender {
    for (UIButton *button in self.typeItems) {
        button.selected = NO;
        button.backgroundColor = [UIColor appBGColor];
        button.layer.borderColor = [[UIColor appBGColor] CGColor];
    }
    self.typeString = sender.titleLabel.text;
    sender.selected = YES;
    sender.backgroundColor = [UIColor colorWithHexString:@"#ECF9F9"];
    sender.layer.borderColor = [[UIColor appGreenColor] CGColor];
}

//请输入户号
- (void)configRHHView {
    UILabel *titleLabel = [[UILabel alloc]initWithFrame:CGRectMake(16, 314, 200, 20)];
    titleLabel.text = @"请输入户号";
    titleLabel.font = [UIFont systemFontOfSize:14];
    titleLabel.textColor = [UIColor colorWithHexString:@"#666666"];
    [self.scrollView addSubview:titleLabel];
    
    self.cardIdField.frame = CGRectMake(16, titleLabel.height+titleLabel.origin.y+10, kScreenWidth-32, 28);
    [self.scrollView addSubview:self.cardIdField];
    
    UIView *lineView = [[UIView alloc]initWithFrame:(CGRect)CGRectMake(16, self.cardIdField.height+self.cardIdField.origin.y+8, kScreenWidth-32, 1)];
    lineView.backgroundColor = [UIColor colorWithHexString:@"#EDEDED"];
    [self.scrollView addSubview:lineView];
}

//缴费金额
- (void)configJFViewWithMoneyItems:(NSArray *)moneyItems {
    UILabel *titleLabel = [[UILabel alloc]initWithFrame:CGRectMake(16, 410, 200, 20)];
    titleLabel.text = @"缴费金额";
    titleLabel.font = [UIFont boldSystemFontOfSize:16];
    titleLabel.textColor = [UIColor appBlack333LabelColor];
    [self.scrollView addSubview:titleLabel];
    
    NSInteger itemCount = moneyItems.count;
    
    CGFloat width = (kScreenWidth-16)/3;
    CGFloat height = 96;
    self.moneyItems = [NSMutableArray arrayWithCapacity:itemCount];
    UIView *lastView ;
    __weak __typeof(&*self)weakSelf = self;
    for (int i = 0; i < itemCount; i ++) {
        HNSH_ExchangeHuaFeiItem *item = [[HNSH_ExchangeHuaFeiItem alloc]initWithFrame:CGRectMake(8+width*(i%3), titleLabel.height+titleLabel.origin.y+10+(height+10)*(i/3), width, height)];
        lastView = item;
        item.select = i == 0;
        if (i == 0) {
            self.currentModel = moneyItems[i];
        }
        item.model = moneyItems[i];
        [self.scrollView addSubview:item];
        [self.moneyItems addObject:item];
        item.clickBlock = ^(HNSH_ExchangeHuaFeiItemModel * _Nonnull selectModel) {
            for (HNSH_ExchangeHuaFeiItem *itemView in weakSelf.moneyItems) {
                itemView.select = NO;
            }
            weakSelf.currentModel = selectModel;
        };
    }
    
    UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
    [button setTitle:@"立即充值" forState:UIControlStateNormal];
    [button setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    button.titleLabel.font = [UIFont boldSystemFontOfSize:16];
    button.backgroundColor = [UIColor appGreenColor];
    [self.scrollView addSubview:button];
    button.layer.cornerRadius = 25.0f;
    button.layer.masksToBounds = YES;
    
    [button mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(lastView.mas_bottom).offset(92);
        make.left.mas_equalTo(30);
        make.height.mas_equalTo(50);
        make.width.mas_equalTo(kScreenWidth-60);
    }];
    [[button rac_signalForControlEvents:UIControlEventTouchUpInside] subscribeNext:^(__kindof UIControl * _Nullable x) {
        if (![weakSelf.addressField.text isNotBlank]) {
            [self.view makeToast:@"请输入所属地区"];
            return;
        }
        if (![weakSelf.dwmcString isNotBlank]) {
            [self.view makeToast:@"请选择供电单位"];
            return;
        }
        if (![weakSelf.typeString isNotBlank]) {
            [self.view makeToast:@"请选择类型"];
            return;
        }
        if (![weakSelf.cardIdField.text isNotBlank]) {
            [self.view makeToast:@"请输入户号"];
            return;
        }
        [weakSelf payMoney];
    }];
}

//MARK: 支付
- (void)payMoney {
    [ST_PayPasswordView showPasswordViewWithSureBlock:^(NSString * _Nonnull password) {
        [self loadPayMoneyWithPwd:password];
    } cancelBlock:^{
        
    }];
}

#pragma mark networking
- (void)loadHomeScrollData {
//    [HNSH_ExchangeNetServer requestExchangeProductConfigWithType:2 success:^(ST_BaseNetModel * _Nullable responseObject) {
//        if (responseObject.code == 0) {
//            NSArray *moneyItems = [NSArray yy_modelArrayWithClass:[HNSH_ExchangeHuaFeiItemModel class] json:responseObject.resultset];
//            [self configJFViewWithMoneyItems:moneyItems];
//        }
//    } Failure:^(NSError * _Nullable error) {
//        
//    }];
}

- (void)loadPayMoneyWithPwd:(NSString *)pwd {
    NSString *extra_data = [NSString stringWithFormat:@"%@/%@/%@",self.addressField.text,self.dwmcString,self.typeString];
    NSDictionary *dic = @{@"id":self.currentModel.uid,
                          @"account_no":self.cardIdField.text,
                          @"pay_password":pwd,
                          @"extra_data":extra_data
    };
    [HNSH_ExchangeNetServer requestExchangeProductSubmitWithInfo:dic success:^(ST_BaseNetModel * _Nullable responseObject) {
        if (responseObject.code == 0) {
            [self.view makeToast:@"充值成功"];
        }
    } Failure:^(NSError * _Nullable error) {
        
    }];
}


#pragma mark lazy
- (UITextField *)addressField {
    if (!_addressField) {
        _addressField = [[UITextField alloc]init];
        _addressField.textColor = [UIColor appBlack333LabelColor];
        _addressField.font = [UIFont boldSystemFontOfSize:20];
        _addressField.placeholder = @"请输入所属地区";
    }
    return _addressField;
}

- (UITextField *)cardIdField {
    if (!_cardIdField) {
        _cardIdField = [[UITextField alloc]init];
        _cardIdField.textColor = [UIColor appBlack333LabelColor];
        _cardIdField.font = [UIFont boldSystemFontOfSize:20];
        _cardIdField.placeholder = @"请输入户号";
        _cardIdField.keyboardType = UIKeyboardTypePhonePad;
    }
    return _cardIdField;
}

- (UIScrollView *)scrollView {
    if (!_scrollView) {
        _scrollView = [[UIScrollView alloc]initWithFrame:CGRectMake(0, kNavBarAndStatusBarHeight, kScreenWidth, kScreenHeight-kNavBarAndStatusBarHeight)];
        _scrollView.contentSize = CGSizeMake(kScreenWidth, 700);
        _scrollView.backgroundColor = [UIColor appWhiteLabelColor];
    }
    return _scrollView;
}

@end
